package com.feicheng.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.common.util.StringGenerator;
import com.feicheng.persistence.entity.Role;
import com.feicheng.persistence.entity.User;
import com.feicheng.persistence.service.impl.RoleServiceImpl;
import com.feicheng.persistence.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/user")
public class UserController {

	@Autowired
	UserServiceImpl userServiceImpl;

	@GetMapping("/page")
	public ResultInfo selectRolesPage(
			String account,
            String tel,
			String secId,
            @RequestParam(defaultValue = "1")Integer current,
            @RequestParam(defaultValue = "10")Integer size) {
		QueryWrapper<User> qw = new QueryWrapper<>();
		Page<User> page = new Page<>(current, size);

		if (StringUtils.hasLength(account)) {
			qw.like("account", account);
		}

		if (StringUtils.hasLength(tel)) {
			qw.like("tel", tel);
		}

		if (StringUtils.hasLength(secId)) {
			qw.like("secId", secId);
		}

		page = userServiceImpl.page(page, qw);

		return ResultInfo.success(page);
	}
	
	@GetMapping("/info")
	public ResultInfo selectUserById(String userId) {
		JSONObject result = new JSONObject();
		result.put("user", userServiceImpl.getById(userId));
		return ResultInfo.success(result);
	}
	
	@PostMapping("/info")
	public ResultInfo addUser(@RequestBody User user) {
		user.setRoles("[]");
		userServiceImpl.save(user);
		return ResultInfo.success(user);
	}
	
	@DeleteMapping("/info")
	public ResultInfo deleteUserById(String userId) {
		User user = userServiceImpl.getById(userId);
		userServiceImpl.removeById(userId);
		return ResultInfo.success(user);
	}
	
	@PutMapping("/info")
	public ResultInfo updateUser(@RequestBody User user) {
		userServiceImpl.updateById(user);
		return ResultInfo.success();
	}

	@PutMapping("/cp")
	public ResultInfo updateCp(@RequestBody JSONObject param) {
		Integer userId = param.getInteger("userId");
		String p = param.getString("password");
		String np = param.getString("np");
		String cp = param.getString("cp");

		User user = userServiceImpl.getById(userId);
		String pStr = StringGenerator.eCodePassword(p, user.getSalt());
		if (pStr.equals(user.getPassword())) {

			if (np.equals(cp)) {
				user.setPassword(StringGenerator.eCodePassword(np, user.getSalt()));
				userServiceImpl.updateById(user);
			} else {
				throw new SystemException("新密码和确认密码不一致");
			}


		} else {
			throw new SystemException("原密码不正确");
		}

		return ResultInfo.success();
	}

}
